package com.guochuang.yunwang.promanager.controller;


import com.github.pagehelper.PageInfo;
import com.guochuang.yunwang.promanager.annotation.CancelLoginIntercept;
import com.guochuang.yunwang.promanager.domain.Group;
import com.guochuang.yunwang.promanager.domain.Project;
import com.guochuang.yunwang.promanager.domain.vo.ProjectVO;
import com.guochuang.yunwang.promanager.service.ProjectService;
import com.guochuang.yunwang.promanager.utils.ProjectConstant;
import com.guochuang.yunwang.promanager.utils.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Api(value = "项目管理", tags = "项目管理")
@Slf4j
public class ProjectController {


    @Autowired
    private ProjectService projectService;


    @GetMapping(value = "/get/project/list/{page}/{offset}")
    @ApiOperation(value = "根据分页查询项目")
    @CancelLoginIntercept
    public ResultEntity getProjectListByPage(@PathVariable Integer page,
                                             @PathVariable Integer offset,
                                             @RequestParam(value = "flag") String flag) {

        PageInfo pageInfo = projectService.getProjectListByPage(page, offset, flag);

        Map<String, Object> rec = new HashMap<>();

        rec.put("total", pageInfo.getTotal());

        rec.put("data", pageInfo.getList());

        return ResultEntity.successWithData(rec);

    }

    @GetMapping(value = "/get/no/group")
    @ApiOperation(value = "获取未分配项目的团队")
    @CancelLoginIntercept
    public ResultEntity getGroup() {

        List<Group> groups = projectService.getGroups();

        return ResultEntity.successWithData(groups);
    }


    @PostMapping(value = "/get/projects/by/params")
    @ApiOperation(value = "根据参数条件查询项目")
    @CancelLoginIntercept
    public ResultEntity getProjectByParams(@RequestBody ProjectVO projectVo) {

        PageInfo pageInfo = projectService.getProjectListByParams(projectVo);

        Map<String, Object> rec = new HashMap<>();

        rec.put("total", pageInfo.getTotal());

        rec.put("data", pageInfo.getList());

        return ResultEntity.successWithData(rec);
    }

    @PostMapping(value = "/select/project/group")
    @ApiOperation(value = "给项目分配团队")
    @CancelLoginIntercept
    public ResultEntity selectProjectGroup(@RequestParam(value = "pid", required = true) Integer pid,
                                           @RequestParam(value = "gid", required = true) Integer gid) {
        Integer nums = projectService.addGroupToProject(pid, gid);

        if (nums > 0) {
            return ResultEntity.successWithoutData();
        } else {
            return ResultEntity.failed(ProjectConstant.USER_CHANGE_FAILED);
        }
    }

    @PostMapping(value = "/add/new/project")
    @ApiOperation(value = "添加新的项目")
    @CancelLoginIntercept
    public ResultEntity addNewProject(@RequestBody Project project) {

        Integer nums = projectService.addNewProject(project);

        if (nums > 0) {
            return ResultEntity.successWithoutData();
        } else {
            return ResultEntity.failed(ProjectConstant.PROJECT_ADD_FAILED);
        }
    }

    @GetMapping(value = "/udt/by/{pid}")
    @ApiOperation(value = "结项")
    @CancelLoginIntercept
    public ResultEntity deleteByPid(@PathVariable Integer pid) {
        Integer nums = projectService.deleteByPid(pid);
        if (nums > 0) {
            return ResultEntity.successWithoutData();
        } else {
            return ResultEntity.failed(ProjectConstant.PROJECT_DELETE_FAILED);
        }
    }

}
